﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Box.Entities;
namespace Box.Repository
{
    public class KitRepository : MasterRepository, IKitRepository
    {
        public List<Kit> GetAllFromKits()
        {
            var query = from c in Context.Kits
                        select c;
            return query.ToList();
        }

        public List<Kit> GetFromKitsByCriterio(string criterio)
        {
            var query = from c in Context.Kits
                        select c;
            if (criterio != null && criterio != string.Empty)
            {
                query = from c in query
                        where c.NombreKit.ToUpper().Contains(criterio.ToUpper()) ||
                              c.DescripcionKit.ToUpper().Contains(criterio.ToUpper())
                        select c;
            }
            return query.ToList();
        }

        public Kit GetFromKitsById(int id)
        {
            return Context.Kits.Find(id);
        }

        public void AddKit(Kit Kit)
        {
            Context.Kits.Add(Kit);
            Context.SaveChanges();
        }

        public void UpdateKit(Kit Kit)
        {
            Context.Entry(Kit).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public void RemoveKit(int id)
        {
            var existe = Context.Kits.Find(id);
            if (existe != null)
            {
                Context.Kits.Remove(existe);
                Context.SaveChanges();
            }
        }

        public bool ExisteKit(int id)
        {
            var query = Context.Kits.Where(p => p.Id.Equals(id));

            return query.Any();
        }
    }
}
